package com.zxwl.common.util;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringEscapeUtils;

public class JsonUtil {
	
	/**
	 * 将一个对象转换成json字符串
	 */
	@SuppressWarnings("rawtypes")
	public static String objectToJson(Object object) 
	{
		StringBuilder json = new StringBuilder();
		if (object == null)
		{
			json.append("\"\"");
		} 
		else if(object instanceof Integer || object instanceof Short)
		{
			json.append("\"").append(object).append("\"");
		}
		else if(object instanceof Double || object instanceof Float)
		{
			json.append("\"").append(object).append("\"");
		}
		else if (object instanceof String) 
		{	
			String value = (String)object;
			// 处理特殊字符
			value = value.replaceAll("\r\n", "");
			value = value.replaceAll("\n", "");
			//value = value.replaceAll("\\s*", "");//处理空格 tab 制表符等空
			value = value.replaceAll("\"", "'");
			value = value.replaceAll("\'", "\\'");
			value = StringEscapeUtils.escapeHtml3(value);
			json.append("\"").append(value).append("\"");
			
		} 
		else if(object instanceof Date)
		{
			json.append("\"").append(formateDate((Date)object)).append("\"");
		}
		else if(object instanceof Boolean)
		{
			json.append("\"").append(object).append("\"");
		}
		else if(object instanceof List)
		{
			json.append(listToJson((List)object));
		}
		else if(object instanceof Map)
		{
			json.append(MaptoJson((Map)object));
		}
		else 
		{	
			json.append(beanToJson(object));
		}
		return json.toString();
	}
	
	/**
	 * 将一个bean对象转换成json字符串
	 */
	public static String beanToJson(Object bean) 
	{
		StringBuilder json = new StringBuilder();
		json.append("{");
		PropertyDescriptor[] props = null;
		try 
		{
			props = Introspector.getBeanInfo(bean.getClass(), Object.class).getPropertyDescriptors();
		} 
		catch (IntrospectionException e) 
		{
		}
		if (props != null) 
		{
			for (int i = 0; i < props.length; i++) 
			{
				try 
				{	
					String name = objectToJson(props[i].getName());
					String value = objectToJson(props[i].getReadMethod().invoke(bean));
					if(name != null && !name.equals(""))
					{
						json.append(name);
						json.append(":");
						json.append(value);
						json.append(",");
					}
				} 
				catch (Exception e) 
				{
				}
			}
			json.setCharAt(json.length() - 1, '}');
		}
		else 
		{
			json.append("}");
		}
		return json.toString();
	}

	/**
	 * 将一个集合转换成json字符串
	 * 
	 */
	public static String listToJson(List<?> list) 
	{
		StringBuilder json = new StringBuilder();
		json.append("[");
		if (list != null && list.size() > 0)
		{
			for (Object obj : list) 
			{	
				json.append(objectToJson(obj));
				json.append(",");
			}
			json.setCharAt(json.length() - 1, ']');
		} 
		else
		{
			json.append("]");
		}
		return json.toString();
	}
	
	/**
	 * 将一个map转换成json字符串
	 */
	public  static  String MaptoJson(Map<?,?> map){
		   Set<?> keys = map.keySet();
		   Object key = null;
		   Object value = null;
		   StringBuffer jsonBuffer = new StringBuffer();
		   jsonBuffer.append("{");
		   for(Iterator<?> it = keys.iterator();it.hasNext();)
		   {
		       key =  it.next();
		       value = map.get(key);
		       if(key == null && value == null)
		       {
		    	   continue;
		       }
		       jsonBuffer.append("\""+key.toString()+"\":" + objectToJson(value));
		       if(it.hasNext())
		       {
		            jsonBuffer.append(",");
		       }
		       
		   }
		   jsonBuffer.append("}");
		   return jsonBuffer.toString();
		}

    public static String replaceBlank(String str) 
    {  
         String dest = "";  
         if (str!=null) 
         {  
             Pattern p = Pattern.compile("\\s*|\t|\r|\n");  
             Matcher m = p.matcher(str);  
             dest = m.replaceAll("");  
         }  
         return dest;  
    } 
    
	
	/**
	 * 格式化时间
	 */
	private static String formateDate(Date date)
	{
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
		if(date != null)
		{
			String value = formatter.format(date);
			return value.replace(" 00:00:00", "");
		}
		return "";
	}    
}
